package org.example.netty.handler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;
import org.example.utils.LogUtils;

import java.util.List;

/**
 * ReplayingDecoder是ByteToMessageDecoder的子类
 */
public class Byte2IntegerReplayDecoder extends ReplayingDecoder {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        // in的类型 == ReplayingDecoderByteBuf，对原先ByteBuf的一个包装
        // readInt内部会去判断 readableBytes() > 4
        int i = in.readInt();
        LogUtils.info("解码出一个整数：" + i);
        // out中每一个元素都会触发后面的channelRead方法
        out.add(i);
    }
}
